<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-15
 * Time: 13:58
 */

namespace App\Http\Controllers\Backstage;


use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\WorkVali;
use App\Model\Backstage\WorkSummary;
use App\Tool\Error;
use Illuminate\Http\Request;

class WorkSummaryController extends Controller
{
    /**
     * 中介公司有独自的工作总结表
     * 工作总结的列表《该中介公司该部门下面的工作总结列表》
     * @param Request $request
     */
    public function workList(Request $request)
    {
        $params = $request->except('token');
        $validator =  WorkVali::listWork($params);
        if (!$validator) {
            return $this->error(Error::PARAM_MISSING);
        }
        $where[] = ['ws_status', 1];
        //  ws_did 部门ID
        if ($params['ws_did'] != 0) {
            $where[] = ['ws_did' , $params['ws_did']];
        }
        //总结人1  点评人2  peopleType      ws_uid 员工ID
        if($params['peopleType'] == 1){
            if ($params['ws_uid'] != 0) {
                $where[] = ['ws_uid' , $params['ws_uid']];
            }
        } else{
            if ($params['ws_uid'] != 0) {
                $where[] = ['ws_commentator_id' , $params['ws_uid']];
            }
        }
//总结日期1    点评日期2   dateType  start_time   end_time
        if ($params['dateType'] == 1) {
            if ($request->input('start_time')) {
                $where[] = ['ws_create_time', '>=', strtotime($params['start_time'].' 00:00:00')];
            }
            if ($request->input('end_time')) {
                $where[] = ['ws_create_time', '<=', strtotime($params['end_time'].' 23:59:59')];
            }
        } else {
            if ($request->input('start_time')) {
                $where[] = ['ws_comment_time', '>=', strtotime($params['start_time'].' 00:00:00')];
            }
            if ($request->input('end_time')) {
                $where[] = ['ws_comment_time', '<=', strtotime($params['end_time'].' 23:59:59')];
            }
        }
        $work  = new WorkSummary();
        $list = $work->workList(env('PAGE_LIMIT_ADMIN'), $where);//工作总结的列表
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    /**点击添加工作总结页面 返回员工的信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function workAddPage()
    {
        $work  = new WorkSummary();
        $title = $work->workAddPage();
        if (!$title) {
            return $this->error(Error::PLEASE_CONTACT_ADMIN);
        }
        $data['title'] = $title;//标题
        $data['lastmonth'] = date('Y-m-d',strtotime('-1 month'));//上个月时间
        $data['month'] = date('Y-m-d',time());//当天时间
        return $this->success('ok', $data);
    }

    /**员工添加工作总结
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function workAdd(Request $request)
    {
        $params = $request->all();
        $vail = WorkVali::work($params);
        if (!$vail) {
            return $this->error(Error::PARAM_MISSING);
        }
        $work = new WorkSummary();
        $res = $work->addWork($params['ws_job_content'], $params['ws_title']);
        if (!$res) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功！');
    }

    /**修改工作总结
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editAdd(Request $request)
    {
        $params = $request->all();
        $vail = WorkVali::edit($params);
        if (!$vail) {
            return $this->error(Error::PARAM_MISSING);
        }
        $work = new WorkSummary();
        $res = $work->editWork($params);
        if (!$res) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功！');
    }

    /**删除到回收站  回收站还原总结  共用这一个方法
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function softDel(Request $request)
    {
        $params = $request->all();
        $vail = WorkVali::del($params);
        if (!$vail) {
            return $this->error(Error::PARAM_MISSING);
        }
        $work = new WorkSummary();
        $res = $work->updateStatus($params['ws_id']);
        if ($res === -1) {
            return $this->error(Error::PLEASE_CONTACT_ADMIN);
        }
        if (!$res) {
            return $this->error(Error::OPERATION_ERROR);
        }
        return $this->success('ok', '操作成功！');
    }

    /**删除工作总结
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function hardDel(Request $request)
    {
        $params = $request->all();
        $vail = WorkVali::del($params);
        if (!$vail) {
            return $this->error(Error::PARAM_MISSING);
        }
        $work = new WorkSummary();
        $res = $work->deleteWork($params['ws_id']);
        if ($res === -1) {
            return $this->error(Error::NOT_PART_RECYCLE);
        }
        if (!$res) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '删除成功！');
    }

    /**
     * 经理点评
     */
    public function managerReview(Request $request)
    {
        $params = $request->all();
        $vail = WorkVali::review($params);
        if (!$vail) {
            return $this->error(Error::PARAM_MISSING);
        }
        $work = new WorkSummary();
        $res = $work->managerReview($params);
        if (!$res) {
            return $this->error(Error::OPERATION_ERROR);
        }
        return $this->success('ok', '操作成功！');
    }

    /**
     * 回收站工作总结的列表
     */
    public function recycleBinList(Request $request)
    {
        $parameter = $request->all();
        $validator =  WorkVali::recycleBin($parameter);
        if (!$validator) {
            return $this->error(Error::PARAM_MISSING);
        }
        $where[] = ['ws_status', '=',  2];
        if ($parameter['start_time']) {
            $where[] = ['ws_update_time', '>=', strtotime($parameter['start_time'].' 00:00:00')];
        }
        if ($parameter['end_time']) {
            $where[] = ['ws_update_time', '<=', strtotime($parameter['end_time'].' 23:59:59')];
        }
        $work = new WorkSummary();
        $list = $work->recycleBinList(env('PAGE_LIMIT_ADMIN'), $where);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }
}